package com.ycj.design.create.factory.factorymethod;

import java.lang.reflect.InvocationTargetException;

/**
 * @author chuanjieyang
 * @date Nov 20, 2018 16:33:47 PM
 */
public class HumanFactory extends AbstractHumanFactory {
    @Override
    public <T extends Human> T createHuman(Class<T> C) {
        //定义一个生产的人种
        Human human =null;
        try {
            //human = (Human) Class.forName(C.getName()).newInstance();//通过Class.forName的newInstance已经过时
            human= C.getDeclaredConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException  e) {
            System.out.println("人种生成错误");
        } catch (NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return (T) human;
    }
}
